﻿namespace Growatt.ShinebusServiceUploadPack.Service.OSS
{
    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    using Growatt.DuiHelpers;
    using Growatt.ShinebusServiceUploadPack.Controllers;
    using Microsoft.Extensions.Logging;

    public class DeleteFilesonCloud
    {
        // 删除文件所需的参数
        public string path = "yourPath"; // 已审核状态需要传，即status=2时，否则为空
        public string folderName = "yourFolderName";
        public string status = "2"; // 1待审核 2已审核 3驳回
        public string jobId = "yourJobId";
        public string softType = "yourSoftType";
        public string id = "yourId";

        public DeleteFilesonCloud(string path, string folderName, string status, string jobId, string softType, string id)
        {
            this.path = path;
            this.folderName = folderName;
            this.status = status;
            this.jobId = jobId;
            this.softType = softType;
            this.id = id;
        }

        public DeleteFilesonCloud() { }

        public async Task<DeleteFilesonCloudRootobject> DeleteFilesonCloudAsync(ILogger<UpdatePackController> logger, string Server = "20.6.1.140:8081")
        {
            // 参数验证
            if (string.IsNullOrEmpty(folderName) || string.IsNullOrEmpty(status) ||
                string.IsNullOrEmpty(jobId) || string.IsNullOrEmpty(softType) || string.IsNullOrEmpty(id))
            {
                logger.LogError("参数不完整，无法发送删除请求。");
                return null;
            }

            // 构建请求内容
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("folderName", folderName),
                new KeyValuePair<string, string>("status", status),
                new KeyValuePair<string, string>("jobId", jobId),
                new KeyValuePair<string, string>("softType", softType),
                new KeyValuePair<string, string>("id", id),
                new KeyValuePair<string, string>("path", path),
            });

            string url = $"http://{Server}/pubData.do?op=deleteHttpFolder";

            logger.LogInformation($"准备发送POST请求到URL: {url}");

            // 创建HttpClient实例并设置超时时间
            using (HttpClient client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromSeconds(30); // 设置超时时间为30秒

                try
                {
                    // 发送POST请求
                    HttpResponseMessage response = await client.PostAsync(url, content);

                    // 检查响应状态码
                    if (response.IsSuccessStatusCode)
                    {
                        // 读取响应内容
                        string responseBody = await response.Content.ReadAsStringAsync();
                        DeleteFilesonCloudRootobject apiResponse = DuiHelperJson.JsonToObject<DeleteFilesonCloudRootobject>(responseBody);
                        logger.LogInformation("删除文件成功，响应内容: " + responseBody);
                        return apiResponse;
                    }
                    else
                    {
                        logger.LogError($"删除文件失败，状态码: {response.StatusCode}");
                    }
                }
                catch (TaskCanceledException e)
                {
                    logger.LogError($"请求超时，错误信息: {e.Message}");
                }
                catch (Exception e)
                {
                    logger.LogError($"请求发生错误，错误信息: {e.Message}");
                }
            }

            return null;
        }
    }

    public class DeleteFilesonCloudRootobject
    {
        public int result { get; set; }
        public string msg { get; set; }
        public string obj { get; set; }
    }
}
